package com.webmeeting.chat.wordchat.events;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;

import com.myframework.utils.jsutil.JavascriptUtil;
import com.myframework.utils.logutil.WMLogger;
import com.webmeeting.chat.wordchat.bl.ChatRoom;
import com.webmeeting.chat.wordchat.dao.entities.User;
import com.webmeeting.chat.wordchat.servlets.WordChatInitServlet;

public class WordChatMessageInbound extends MessageInbound {

	// current user
	private User				currUser;
	// current chat room name
	private String			roomName;
	// current request
	private HttpSession	session;

	public WordChatMessageInbound(HttpSession session) {
		super();
		this.session = session;
	}

	@Override
	protected void onTextMessage(CharBuffer msg) throws IOException {
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();
		for (ChatRoom room : rooms) {
			if (this.roomName.equals(room.getRoomName())) {
				List<WordChatMessageInbound> users = room.getUsers();
				String message = this.currUser.getNickname() + "|" + msg.toString();
				WMLogger.debug("[Room " + this.roomName + "] " + this.currUser.getNickname() + ": " + msg.toString());
				for (WordChatMessageInbound user : users) {
					CharBuffer buffer = CharBuffer.wrap(message);
					WsOutbound out = user.getWsOutbound();
					out.writeTextMessage(buffer);
					out.flush();
				}
				return;
			}
		}
	}

	@Override
	protected void onClose(int status) {
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();
		for (ChatRoom room : rooms) {
			if (this.roomName.equals(room.getRoomName())) {
				room.getUsers().remove(this);
				WMLogger.debug(this.currUser.getUsername() + " quit the chat room: " + this.roomName);
				return;
			}
		}
		super.onClose(status);
	}

	@Override
	protected void onOpen(WsOutbound outbound) {
		super.onOpen(outbound);
		User currUser = (User) session.getAttribute("user");
		String roomName = (String) session.getAttribute("roomName");
		this.currUser = currUser;
		this.roomName = roomName;
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();
		for (ChatRoom room : rooms) {
			if (this.roomName.equals(room.getRoomName())) {
				room.getUsers().add(this);
				WMLogger.debug(this.currUser.getUsername() + " join the chat room: " + this.roomName);
				return;
			}
		}
	}

	@Override
	protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException {
		// Do nothing here for now
		return;
	}

	public User getCurrUser() {
		return currUser;
	}

	public void setCurrUser(User currUser) {
		this.currUser = currUser;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

}
